#include "formatdescriptions.h"
#include "texteditorconstants.h"

#include <QtGui/qapplication.h>
#include <QtGui/qpalette.h>

namespace TextEditor {
	

FormatDescription::FormatDescription(const QString &id, const QString &displayName, const QColor &color) :
m_id(id),
m_displayName(displayName)
{
	m_format.setForeground(color);
}

QColor FormatDescription::foreground() const
{
	if (m_id == QLatin1String(Constants::C_LINE_NUMBER)) {
		const QColor bg = QApplication::palette().background().color();
		if (bg.value() < 128) {
			return QApplication::palette().foreground().color();
		} else {
			return QApplication::palette().dark().color();
		}
	} else if (m_id == QLatin1String(Constants::C_CURRENT_LINE_NUMBER)) {
		const QColor bg = QApplication::palette().background().color();
		if (bg.value() < 128) {
			return QApplication::palette().foreground().color();
		} else {
			return m_format.foreground();
		}
	} else if (m_id == QLatin1String(Constants::C_OCCURRENCES_UNUSED)) {
		return Qt::darkYellow;
	} else if (m_id == QLatin1String(Constants::C_PARENTHESES)) {
		return QColor(Qt::red);
	}
	return m_format.foreground();
}

QColor FormatDescription::background() const
{
	if (m_id == QLatin1String(Constants::C_TEXT))
		return Qt::white;
	else if (m_id == QLatin1String(Constants::C_LINE_NUMBER))
		return QApplication::palette().background().color();
	else if (m_id == QLatin1String(Constants::C_SEARCH_RESULT))
		return QColor(0xffef0b);
	else if (m_id == QLatin1String(Constants::C_PARENTHESES))
		return QColor(0xb4, 0xee, 0xb4);
	else if (m_id == QLatin1String(Constants::C_CURRENT_LINE)
		|| m_id == QLatin1String(Constants::C_SEARCH_SCOPE)) {
			const QPalette palette = QApplication::palette();
			const QColor &fg = palette.color(QPalette::Highlight);
			const QColor &bg = palette.color(QPalette::Base);

			qreal smallRatio;
			qreal largeRatio;
			if (m_id == QLatin1String(Constants::C_CURRENT_LINE)) {
				smallRatio = .3;
				largeRatio = .6;
			} else {
				smallRatio = .05;
				largeRatio = .4;
			}
			const qreal ratio = ((palette.color(QPalette::Text).value() < 128)
				^ (palette.color(QPalette::HighlightedText).value() < 128)) ? smallRatio : largeRatio;

			const QColor &col = QColor::fromRgbF(fg.redF() * ratio + bg.redF() * (1 - ratio),
				fg.greenF() * ratio + bg.greenF() * (1 - ratio),
				fg.blueF() * ratio + bg.blueF() * (1 - ratio));
			return col;
	} else if (m_id == QLatin1String(Constants::C_SELECTION)) {
		const QPalette palette = QApplication::palette();
		return palette.color(QPalette::Highlight);
	} else if (m_id == QLatin1String(Constants::C_OCCURRENCES)) {
		return QColor(180, 180, 180);
	} else if (m_id == QLatin1String(Constants::C_OCCURRENCES_RENAME)) {
		return QColor(255, 100, 100);
	} else if (m_id == QLatin1String(Constants::C_DISABLED_CODE)) {
		return QColor(239, 239, 239);
	}
	return QColor(); // invalid color
}

}